import calendar

from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import User

from garetjax.db.fields import AutoSlugField
from garetjax.taxonomy.models import Tag, MachineTag, Category

class SimplePost(models.Model):
    title = models.CharField(max_length=100)
    raw_body = models.TextField(_('content'), blank=True)
    content = models.TextField(blank=True, editable=False)
    date_posted = models.DateTimeField(auto_now_add=True)
    author = models.ForeignKey(User, editable=False, null=True, blank=True)
    
    def save(self):
        import markdown2 as markdown
        self.content = markdown.markdown(self.raw_body)
        
        super(SimplePost, self).save()
    
    class Meta:
        ordering = ('-date_posted',)
    
    def __unicode__(self):
        return self.title

class Post(SimplePost):
    MONTHS = ['',
        'jan', 'feb', 'mar', 'apr', 'may', 'jun',
        'jul', 'aug', 'sep', 'oct', 'nov', 'dec'
    ]
    
    slug = AutoSlugField(populate_from='title')
    tags = models.ManyToManyField(Tag, null=True, blank=True)
    machine_tags = models.ManyToManyField(MachineTag, null=True, blank=True)
    category = models.ForeignKey(Category, null=True, blank=True)
    is_commentable = models.BooleanField(default=True)
    
    def get_absolute_url(self):
        return ('blog.display_post', (), {
            'year': self.date_posted.year,
            'month': Post.MONTHS[self.date_posted.month],
            'day': '%02d' % self.date_posted.day,
            'slug': self.slug,
        })
    get_absolute_url = models.permalink(get_absolute_url)